Working With Data

IPython has a lot of power when working with our data. We can view it in various ways, save it to disk, restore it orm the save files and explore it using the IPython tools.

Let's start by creating some variables. I will create some in each of the most useful types.

Start with some numbers.


In [1]:
num = 42

In [2]:
flt = 4.2

Now for a string or two.


In [3]:
strng = "Some text"

In [4]:
m_strng = "Text with you're single quote"

In [5]:
m_strng


Out[5]:
"Text with you're single quote"

Some arrays.


In [6]:
an_arr = [3, 5, 7, 9, 11, 13, 15, 17, 19]

In [7]:
other_arr = [12, "word", 6, 22, "Tate", 17]

In [8]:
other_arr


Out[8]:
[12, 'word', 6, 22, 'Tate', 17]

Hey, how about that, an array contain items of more than one type.

Now dictionaries.


In [9]:
a_dict = {'first': "Word", 'second': "Three words total", 'third': 84, 'fourth': 91}

In [10]:
b_dict = {1: "twist", 7: "not six"}

In [11]:
c_dict = {'key':'item', 22: 'more_item'}

Wow, dictionaries are really loose. You can mix types in both the key and the items. Let's look inside a couple.


In [12]:
a_dict


Out[12]:
{'first': 'Word', 'fourth': 91, 'second': 'Three words total', 'third': 84}

Though it seems the order isn't maintained.


In [13]:
a_dict['second']


Out[13]:
'Three words total'

In [14]:
a_dict['fourth']


Out[14]:
91

In [15]:
a_dict['fourth'] = 31

In [16]:
a_dict


Out[16]:
{'first': 'Word', 'fourth': 31, 'second': 'Three words total', 'third': 84}

I wonder what variables I have declared so far?


In [17]:
who


a_dict	 an_arr	 b_dict	 c_dict	 flt	 m_strng	 num	 other_arr	 strng	 

What can I find out about a variable?


In [18]:
a_dict?
Type:        dict
String form: {'second': 'Three words total', 'fourth': 91, 'third': 84, 'first': 'Word'}
Length:      4
Docstring:
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
    (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
        d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list.  For example:  dict(one=1, two=2)

In [19]:
type(flt)


Out[19]:
float

In [20]:
pdoc b_dict
Class docstring:
    dict() -> new empty dictionary
    dict(mapping) -> new dictionary initialized from a mapping object's
        (key, value) pairs
    dict(iterable) -> new dictionary initialized as if via:
        d = {}
        for k, v in iterable:
            d[k] = v
    dict(**kwargs) -> new dictionary initialized with the name=value pairs
        in the keyword argument list.  For example:  dict(one=1, two=2)

In [21]:
pinfo c_dict
Type:        dict
String form: {22: 'more_item', 'key': 'item'}
Length:      2
Docstring:
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
    (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
        d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list.  For example:  dict(one=1, two=2)

In [22]:
who_ls


Out[22]:
['a_dict',
 'an_arr',
 'b_dict',
 'c_dict',
 'flt',
 'm_strng',
 'num',
 'other_arr',
 'strng']

How about keeping values across sessions? We can also clear out some or all of our variables.


In [23]:
store a_dict


Stored 'a_dict' (dict)

In [24]:
store b_dict


Stored 'b_dict' (dict)

In [25]:
store c_dict


Stored 'c_dict' (dict)

In [26]:
who


a_dict	 an_arr	 b_dict	 c_dict	 flt	 m_strng	 num	 other_arr	 strng	 


In [27]:
reset_selective -f [abc]_dict

In [28]:
who


an_arr	 flt	 m_strng	 num	 other_arr	 strng	 

In [29]:
store -r

In [30]:
who


a_dict	 an_arr	 b_dict	 c_dict	 flt	 i	 m_strng	 num	 other_arr	 
strng	 

In [31]:
reset -f

In [32]:
who


Interactive namespace is empty.

In [33]:
store -r

In [34]:
who


a_dict	 an_arr	 b_dict	 c_dict	 i	 other_arr	 

In [35]:
an_arr = [3, 5, 7, 9, 11, 13, 15, 17, 19]

In [36]:
other_arr = [12, "word", 6, 22, "Tate", 17]

In [37]:
who


a_dict	 an_arr	 b_dict	 c_dict	 i	 other_arr	 

In [38]:
l = %who_ls

In [39]:
l


Out[39]:
['a_dict', 'an_arr', 'b_dict', 'c_dict', 'i', 'other_arr']

In [40]:
for i in l:
    t = "store " + i
    get_ipython().magic(t)


Stored 'a_dict' (dict)
Stored 'an_arr' (list)
Stored 'b_dict' (dict)
Stored 'c_dict' (dict)
Stored 'i' (str)
Stored 'other_arr' (list)

In [41]:
who


a_dict	 an_arr	 b_dict	 c_dict	 i	 l	 other_arr	 t	 

In [42]:
reset


Once deleted, variables cannot be recovered. Proceed (y/[n])? y

In [43]:
who


Interactive namespace is empty.

In [44]:
store -r

In [45]:
who


a_dict	 an_arr	 b_dict	 c_dict	 i	 other_arr	 

In [ ]: